package pers.xhh.shopapi.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.xhh.shopapi.annotation.UserLoginToken;
import pers.xhh.shopapi.common.Mi;
import pers.xhh.shopapi.entity.to.attribute.MiAttributeTo;
import pers.xhh.shopapi.service.MiAttributeService;

import javax.validation.Valid;

/**
 * @author wei-xhh
 * @date 2020-11-26
 */
@CrossOrigin
@RestController
@RequestMapping("/api/v1")
public class MiAttributeController {

    @Autowired
    MiAttributeService miAttributeService;

    @UserLoginToken
    @GetMapping("/categories/{id}/attributes")
    public Mi getAttrByCatIdAndSel(@PathVariable Integer id,
                                   @RequestParam(value = "sel") String sel){
        return miAttributeService.getAttrByCatIdAndSel(id, sel);
    }

    @UserLoginToken
    @PostMapping("/categories/{id}/attributes")
    public Mi insertAttr(@PathVariable Integer id,
                                   @Valid @RequestBody MiAttributeTo miAttributeTo){
        return miAttributeService.insertAttr(id, miAttributeTo);
    }

    @UserLoginToken
    @DeleteMapping("/categories/{catId}/attributes/{attrId}")
    public Mi deleteAttr(@PathVariable Integer catId,
                         @PathVariable Integer attrId){
        return miAttributeService.deleteAttr(catId, attrId);
    }

    @UserLoginToken
    @GetMapping("/categories/{catId}/attributes/{attrId}")
    public Mi getAttr(@PathVariable Integer catId,
                         @PathVariable Integer attrId){
        return miAttributeService.getAttr(catId, attrId);
    }

    @UserLoginToken
    @PutMapping("/categories/{catId}/attributes/{attrId}")
    public Mi updateAttr(@PathVariable Integer catId,
                         @PathVariable Integer attrId,
                         @Valid @RequestBody MiAttributeTo miAttributeTo){
        return miAttributeService.updateAttr(catId, attrId, miAttributeTo);
    }


}
